home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / File / Copy.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  9.6 KB  |  311 lines

  1.  
  2. package File::Copy;
  3.  
  4. use strict;
  5. use Carp;
  6. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $Too_Big
  7.         © &syscopy &cp &mv);
  8.  
  9. $VERSION = '2.02';
  10.  
  11. require Exporter;
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(copy move);
  14. @EXPORT_OK = qw(cp mv);
  15.  
  16. $Too_Big = 1024 * 1024 * 2;
  17.  
  18. sub _catname { #  Will be replaced by File::Spec when it arrives
  19.     my($from, $to) = @_;
  20.     if (not defined &basename) {
  21.     require File::Basename;
  22.     import  File::Basename 'basename';
  23.     }
  24.     if ($^O eq 'VMS')  { $to = VMS::Filespec::vmspath($to) . basename($from); }
  25.     elsif ($^O eq 'MacOS') { $to .= ':' . basename($from); }
  26.     elsif ($to =~ m|\\|)   { $to .= '\\' . basename($from); }
  27.     else                   { $to .= '/' . basename($from); }
  28. }
  29.  
  30. sub copy {
  31.     croak("Usage: copy(FROM, TO [, BUFFERSIZE]) ")
  32.       unless(@_ == 2 || @_ == 3);
  33.  
  34.     my $from = shift;
  35.     my $to = shift;
  36.  
  37.     my $from_a_handle = (ref($from)
  38.              ? (ref($from) eq 'GLOB'
  39.                 || UNIVERSAL::isa($from, 'GLOB')
  40.                             || UNIVERSAL::isa($from, 'IO::Handle'))
  41.              : (ref(\$from) eq 'GLOB'));
  42.     my $to_a_handle =   (ref($to)
  43.              ? (ref($to) eq 'GLOB'
  44.                 || UNIVERSAL::isa($to, 'GLOB')
  45.                             || UNIVERSAL::isa($to, 'IO::Handle'))
  46.              : (ref(\$to) eq 'GLOB'));
  47.  
  48.     if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
  49.     $to = _catname($from, $to);
  50.     }
  51.  
  52.     if (defined &syscopy && \&syscopy != \©
  53.     && !$to_a_handle
  54.     && !($from_a_handle && $^O eq 'os2'))    # OS/2 cannot handle handles
  55.     {
  56.     return syscopy($from, $to);
  57.     }
  58.  
  59.     my $closefrom = 0;
  60.     my $closeto = 0;
  61.     my ($size, $status, $r, $buf);
  62.     local(*FROM, *TO);
  63.     local($\) = '';
  64.  
  65.     if ($from_a_handle) {
  66.     *FROM = *$from{FILEHANDLE};
  67.     } else {
  68.     $from = "./$from" if $from =~ /^\s/;
  69.     open(FROM, "< $from\0") or goto fail_open1;
  70.     binmode FROM or die "($!,$^E)";
  71.     $closefrom = 1;
  72.     } 
  73.  
  74.     if ($to_a_handle) {
  75.     *TO = *$to{FILEHANDLE};
  76.     } else {        
  77.     $to = "./$to" if $to =~ /^\s/;
  78.     open(TO,"> $to\0") or goto fail_open2;
  79.     binmode TO or die "($!,$^E)";
  80.     $closeto = 1;
  81.     }  
  82.  
  83.     if (@_) {
  84.     $size = shift(@_) + 0;
  85.     croak("Bad buffer size for copy: $size\n") unless ($size > 0);
  86.     } else {
  87.     $size = -s FROM;
  88.     $size = 1024 if ($size < 512);
  89.     $size = $Too_Big if ($size > $Too_Big);
  90.     }
  91.  
  92.     $! = 0;
  93.     for (;;) {
  94.     my ($r, $w, $t);
  95.     defined($r = sysread(FROM, $buf, $size))
  96.         or goto fail_inner;
  97.     last unless $r;
  98.     for ($w = 0; $w < $r; $w += $t) {
  99.         $t = syswrite(TO, $buf, $r - $w, $w)
  100.         or goto fail_inner;
  101.     }
  102.     }
  103.  
  104.     close(TO) || goto fail_open2 if $closeto;
  105.     close(FROM) || goto fail_open1 if $closefrom;
  106.  
  107.     return 1;
  108.     
  109.   fail_inner:
  110.     if ($closeto) {
  111.     $status = $!;
  112.     $! = 0;
  113.     close TO;
  114.     $! = $status unless $!;
  115.     }
  116.   fail_open2:
  117.     if ($closefrom) {
  118.     $status = $!;
  119.     $! = 0;
  120.     close FROM;
  121.     $! = $status unless $!;
  122.     }
  123.   fail_open1:
  124.     return 0;
  125. }
  126.  
  127. sub move {
  128.     my($from,$to) = @_;
  129.     my($copied,$fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
  130.  
  131.     if (-d $to && ! -d $from) {
  132.     $to = _catname($from, $to);
  133.     }
  134.  
  135.     ($tosz1,$tomt1) = (stat($to))[7,9];
  136.     $fromsz = -s $from;
  137.     if ($^O eq 'os2' and defined $tosz1 and defined $fromsz) {
  138.       unlink $to;
  139.     }
  140.     return 1 if rename $from, $to;
  141.  
  142.     ($sts,$ossts) = ($! + 0, $^E + 0);
  143.     return 1 if defined($fromsz) && !-e $from &&           # $from disappeared
  144.                 (($tosz2,$tomt2) = (stat($to))[7,9]) &&    # $to's there
  145.                 ($tosz1 != $tosz2 or $tomt1 != $tomt2) &&  #   and changed
  146.                 $tosz2 == $fromsz;                         # it's all there
  147.  
  148.     ($tosz1,$tomt1) = (stat($to))[7,9];  # just in case rename did something
  149.     return 1 if ($copied = copy($from,$to)) && unlink($from);
  150.   
  151.     ($tosz2,$tomt2) = ((stat($to))[7,9],0,0) if defined $tomt1;
  152.     unlink($to) if !defined($tomt1) or $tomt1 != $tomt2 or $tosz1 != $tosz2;
  153.     ($!,$^E) = ($sts,$ossts);
  154.     return 0;
  155. }
  156.  
  157. *cp = \©
  158. *mv = \&move;
  159.  
  160. *syscopy = ($^O eq 'VMS' ? \&rmscopy : \©) unless defined &syscopy;
  161.  
  162. 1;
  163.  
  164. __END__
  165.  
  166. =head1 NAME
  167.  
  168. File::Copy - Copy files or filehandles
  169.  
  170. =head1 SYNOPSIS
  171.  
  172.       use File::Copy;
  173.  
  174.     copy("file1","file2");
  175.       copy("Copy.pm",\*STDOUT);'
  176.     move("/dev1/fileA","/dev2/fileB");
  177.  
  178.       use POSIX;
  179.     use File::Copy cp;
  180.  
  181.     $n=FileHandle->new("/dev/null","r");
  182.     cp($n,"x");'
  183.  
  184. =head1 DESCRIPTION
  185.  
  186. The File::Copy module provides two basic functions, C<copy> and
  187. C<move>, which are useful for getting the contents of a file from
  188. one place to another.
  189.  
  190. =over 4
  191.  
  192. =item *
  193.  
  194. The C<copy> function takes two
  195. parameters: a file to copy from and a file to copy to. Either
  196. argument may be a string, a FileHandle reference or a FileHandle
  197. glob. Obviously, if the first argument is a filehandle of some
  198. sort, it will be read from, and if it is a file I<name> it will
  199. be opened for reading. Likewise, the second argument will be
  200. written to (and created if need be).
  201.  
  202. B<Note that passing in
  203. files as handles instead of names may lead to loss of information
  204. on some operating systems; it is recommended that you use file
  205. names whenever possible.>  Files are opened in binary mode where
  206. applicable.  To get a consistent behavour when copying from a
  207. filehandle to a file, use C<binmode> on the filehandle.
  208.  
  209. An optional third parameter can be used to specify the buffer
  210. size used for copying. This is the number of bytes from the
  211. first file, that wil be held in memory at any given time, before
  212. being written to the second file. The default buffer size depends
  213. upon the file, but will generally be the whole file (up to 2Mb), or
  214. 1k for filehandles that do not reference files (eg. sockets).
  215.  
  216. You may use the syntax C<use File::Copy "cp"> to get at the
  217. "cp" alias for this function. The syntax is I<exactly> the same.
  218.  
  219. =item *
  220.  
  221. The C<move> function also takes two parameters: the current name
  222. and the intended name of the file to be moved.  If the destination
  223. already exists and is a directory, and the source is not a
  224. directory, then the source file will be renamed into the directory
  225. specified by the destination.
  226.  
  227. If possible, move() will simply rename the file.  Otherwise, it copies
  228. the file to the new location and deletes the original.  If an error occurs
  229. during this copy-and-delete process, you may be left with a (possibly partial)
  230. copy of the file under the destination name.
  231.  
  232. You may use the "mv" alias for this function in the same way that
  233. you may use the "cp" alias for C<copy>.
  234.  
  235. =back
  236.  
  237. File::Copy also provides the C<syscopy> routine, which copies the
  238. file specified in the first parameter to the file specified in the
  239. second parameter, preserving OS-specific attributes and file
  240. structure.  For Unix systems, this is equivalent to the simple
  241. C<copy> routine.  For VMS systems, this calls the C<rmscopy>
  242. routine (see below).  For OS/2 systems, this calls the C<syscopy>
  243. XSUB directly.
  244.  
  245. =head2 Special behavior if C<syscopy> is defined (VMS and OS/2)
  246.  
  247. If both arguments to C<copy> are not file handles,
  248. then C<copy> will perform a "system copy" of
  249. the input file to a new output file, in order to preserve file
  250. attributes, indexed file structure, I<etc.>  The buffer size
  251. parameter is ignored.  If either argument to C<copy> is a
  252. handle to an opened file, then data is copied using Perl
  253. operators, and no effort is made to preserve file attributes
  254. or record structure.
  255.  
  256. The system copy routine may also be called directly under VMS and OS/2
  257. as C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
  258. is the routine that does the actual work for syscopy).
  259.  
  260. =over 4
  261.  
  262. =item rmscopy($from,$to[,$date_flag])
  263.  
  264. The first and second arguments may be strings, typeglobs, typeglob
  265. references, or objects inheriting from IO::Handle;
  266. they are used in all cases to obtain the
  267. I<filespec> of the input and output files, respectively.  The
  268. name and type of the input file are used as defaults for the
  269. output file, if necessary.
  270.  
  271. A new version of the output file is always created, which
  272. inherits the structure and RMS attributes of the input file,
  273. except for owner and protections (and possibly timestamps;
  274. see below).  All data from the input file is copied to the
  275. output file; if either of the first two parameters to C<rmscopy>
  276. is a file handle, its position is unchanged.  (Note that this
  277. means a file handle pointing to the output file will be
  278. associated with an old version of that file after C<rmscopy>
  279. returns, not the newly created version.)
  280.  
  281. The third parameter is an integer flag, which tells C<rmscopy>
  282. how to handle timestamps.  If it is E<lt> 0, none of the input file's
  283. timestamps are propagated to the output file.  If it is E<gt> 0, then
  284. it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
  285. timestamps other than the revision date are propagated; if bit 1
  286. is set, the revision date is propagated.  If the third parameter
  287. to C<rmscopy> is 0, then it behaves much like the DCL COPY command:
  288. if the name or type of the output file was explicitly specified,
  289. then no timestamps are propagated, but if they were taken implicitly
  290. from the input filespec, then all timestamps other than the
  291. revision date are propagated.  If this parameter is not supplied,
  292. it defaults to 0.
  293.  
  294. Like C<copy>, C<rmscopy> returns 1 on success.  If an error occurs,
  295. it sets C<$!>, deletes the output file, and returns 0.
  296.  
  297. =back
  298.  
  299. =head1 RETURN
  300.  
  301. All functions return 1 on success, 0 on failure.
  302. $! will be set if an error was encountered.
  303.  
  304. =head1 AUTHOR
  305.  
  306. File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
  307. and updated by Charles Bailey I<E<lt>bailey@genetics.upenn.eduE<gt>> in 1996.
  308.  
  309. =cut
  310.  
  311.